<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <style>
        * {
            padding: 0;
            margin: 0;
        }
        
        #bg {
            position: fixed;
            width: 100%;
            height: 100%;
            top: 0;
            left: 0;
            background: rgba(0, 0, 0, 0.2);
            display: none;
        }
        
        #login {
            background-color: white;
            width: 600px;
            height: 300px;
            position: absolute;
            top: 50%;
            left: 50%;
            z-index: 2;
            text-align: center;
            border: 1px solid black;
            display: none;
            transform: translate(-50%, -50%);
        }
        
        #guanbi {
            position: absolute;
            text-align: center;
            line-height: 50px;
            background-color: white;
            width: 50px;
            height: 50px;
            border-radius: 255px;
            border: 1px solid black;
            top: -15px;
            right: -15px;
            z-index: 3;
        }
        
        #login {
            cursor: move;
        }
    </style>

</head>

<body>
    <h2 style="text-align: center;"><a id="link" href="#" style="text-decoration: none;color: black;">点击、弹出登录框</a></h2>
    <div id="login">
        <div id="title">
            <h3 style="margin-top: 30px;">登录会员</h3>
        </div>
        <div style="margin-top: 30px;">
            <span style="display: inline-block;width: 100px;font-size: 16px;">用户名:</span><input type="text" style="width: 250px;height: 30px;" placeholder="请输入用户名">
        </div>


        <div style="margin-top: 30px;">
            <span style="display: inline-block;width: 100px;font-size: 16px;">登陆密码:</span><input type="text" style="width: 250px;height: 30px;" placeholder="请输入密码">

        </div>
        <button style="width: 200px;height: 40px;;border: none;border: 1px solid black;margin-top: 30px;background-color: white;">会员登陆</button>

        <div id="guanbi"><a href="#">×</a></div>
    </div>

    <div id="bg">

    </div>



</body>
<script>
    var login = document.getElementById('login');
    var h2 = document.getElementsByTagName('h2');
    var bg = document.getElementById('bg');
    var guanbi = document.getElementById('guanbi');
    var title = document.getElementById('title');

    var link = document.getElementById('link');
    link.addEventListener('click', function() {
        login.style = 'display: block';
        bg.style = 'display: block';
        h2[0].style.display = 'none';
    })


    guanbi.addEventListener('click', function() {
        login.style = 'display: none';
        bg.style = 'display: none';
        h2[0].style.display = ' block';

    })


    /*  login.addEventListener('mousedown', function(e) {
         var x = e.pageX - login.offsetLeft;
         var y = e.pageY - login.offsetTop;
         console.log('(' + (x) + ',' + (y) + ')');
         var move = document.addEventListener('mousemove', function(e) {
             login.style.left = e.pageX - x + 'px';
             login.style.top = e.pageY - y + 'px';
         })
     })


     login.addEventListener('mouseleave', function(e) {
         document.removeEventListener('mousemove', move);
     }) */

    login.addEventListener('mousedown', function(e) {
        var x = e.pageX - login.offsetLeft;
        var y = e.pageY - login.offsetTop;
        // (2) 鼠标移动的时候，把鼠标在页面中的坐标，减去 鼠标在盒子内的坐标就是模态框的left和top值
        document.addEventListener('mousemove', move);

        function move(e) {
            login.style.left = e.pageX - x + 'px';
            login.style.top = e.pageY - y + 'px';
        }
        // (3) 鼠标弹起，就让鼠标移动事件移除
        document.addEventListener('mouseup', function() {
            document.removeEventListener('mousemove', move);
        })
    })
</script>


</html>